package com.demo.petshop.controller;

import com.demo.petshop.service.VideosService;
import com.demo.petshop.util.Result;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static com.demo.petshop.entity.table.VideoTagRelationTableDef.VIDEO_TAG_RELATION;
import static com.demo.petshop.entity.table.VideoTagsTableDef.VIDEO_TAGS;
import static com.demo.petshop.entity.table.VideosTableDef.VIDEOS;

import java.util.Map;

/**
 *  控制层。
 *
 * @author lenovo
 * @since 2025-06-09
 */
@RestController
@RequestMapping("/videos")
public class VideosController {

    @Autowired
    private VideosService videosService;

    @GetMapping("categories")
    public Object categories(){
        return videosService.categories();
    }

    @GetMapping("list")
    public Object list(@RequestParam Map<String, Object> params){
        return Result.success(videosService.list(new QueryWrapper()
                .select()
                .from(VIDEO_TAGS, VIDEOS, VIDEO_TAG_RELATION)
                .where(VIDEO_TAG_RELATION.VIDEO_ID.eq(VIDEOS.VIDEO_ID))
                .and(VIDEO_TAG_RELATION.TAG_ID.eq(VIDEO_TAGS.TAG_ID))
                .and(VIDEO_TAGS.TAG_ID.eq(params.get("category")))
                .and(VIDEO_TAGS.NAME.like(params.get("query")))
        ));
    }
}
